﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.Details
{
    public partial class Subject_PointColumnUC : DetailUC
    {
        public Subject_PointColumnUC()
        {
            InitializeComponent();
            this.ObjectName = "cột điểm";
            this.Code = "Subject_PointColumn";
            this.Type = new Subject_PointColumn();

            comboboxUC_point.Model = RegisterService.RegisterServices.PointColumnService;
            comboboxUC_point.Uc = new PointColumnUC();
            comboboxUC_point.ColumnMain = "Name";
            comboboxUC_point.ColumnDisplay = "Code,Name,Description";
        }

        protected override long DetailDisplay()
        {
            Subject_PointColumn data = new Subject_PointColumn();
            if (ID > 0)
            {
                data = (Subject_PointColumn)RegisterService.RegisterServices.SubjectPointColumnService.GetByID(ID);

            }

            comboboxUC_point.ID = data.PointColumnID;
            return ID;

        }

        protected override long Save()
        {
            Subject_PointColumn data = new Subject_PointColumn();
            if (ID > 0)
            {
                data = (Subject_PointColumn)RegisterService.RegisterServices.SubjectPointColumnService.GetByID(ID);

            }

            data.PointColumnID = comboboxUC_point.ID;
            data.SubjectID = long.Parse(SearchString.Replace("'", "").Replace("SubjectID=", "").Trim());

            ID = RegisterService.RegisterServices.SubjectPointColumnService.Save(data);

            //truong hop neu them moi 1 cot diem vao mon hoc thi them luon vao bang keu qua
            if (data.ID <= 0)
            {
                //lay branch ID của môn học
                var branchID1 = ((Subject)RegisterService.RegisterServices.SubjectService.GetByID(data.SubjectID)).BranchID;
               //lay danh sach hoc vien theo branchid
                var student = RegisterService.RegisterServices.StudentService.GetList(" br.ID=" + branchID1) as List<Student>;
                foreach (var item in student)
                {
                    Student_Result st = new Student_Result();
                    st.StudentID = item.ID;
                    st.Subject_PointColumnID = ID;
                    st.Value = 0;
                    RegisterService.RegisterServices.StudentResultService.Save(st);
                }
            }

            return ID;
        }


        public override bool Delete(long SelectedID)
        {
            var check = RegisterService.RegisterServices.StudentResultService.GetBySubjectPointColumnID(SelectedID);
            if (check != null && check.Count != 0)
            {
                if (MessageControl.ShowMessageWithResult("Việc xóa cột điểm theo môn học này sẽ ảnh hưởng đến việc hiển thị kết quả của học viên, bạn có chắc muốn xóa ?", "Warning", MessageControl.Icon.Warning) != DialogResult.OK)
                {
                    return false;
                }
            }

            return RegisterService.RegisterServices.SubjectPointColumnService.UpdateIsDelete(RegisterService.RegisterServices.SubjectPointColumnService.GetByID(SelectedID));
            
         //   return RegisterService.RegisterServices.SubjectPointColumnService.Delete(RegisterService.RegisterServices.SubjectPointColumnService.GetByID(SelectedID));

        }

        private void Subject_PointColumnUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(groupControl1.Controls);
        }
    }
}
